<?php include HEADER; ?>
<script src="<?php echo __URL_BASE ?>js/jquery.validate.min.js" type="text/javascript"></script>
<script src="<?php echo __URL_BASE ?>js/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<link href="<?php echo __URL_BASE ?>css/cart.css" rel="stylesheet" type="text/css">

  <div id="m-update-cart">
    <div class="p-cart-info">

      <h4 class="title"> <span>Thông tin đơn hàng</span><span class="border"></span></h4>
<?php if(isset($cart)) :
		$TongTien = 0; // nếu có sản phẩm trong giỏ hàng thì khởi tạo biến tính tổng tiền
		$PhuongThucThanhToanModel = new phuongthucthanhtoan();
		$lstpttt = $PhuongThucThanhToanModel->getPhuongThucThanhToanByWhere('TrangThai = 1',''); 
?>
	<form action="<?php echo __URL_BASE.'index.php?rt=cart/updatecart' ?>" method="post" >
      <table class="p-cart-order">
        <tbody>
          <tr>
            <th> Ảnh </th>
            <th style="width: 150px;"> Tên </th>
            <th>Số lượng</th>
            <th> Giá khuyến mại </th>
            <th> Thành tiền </th>
            <th style="width: 35px;"> Xóa </th>
          </tr>
  <?php foreach($cart as $tivi_id=>$soluong): 
			$tiviModel = new tivi();
			$tivi = $tiviModel->getTiviById($tivi_id);
			$TongTien += $tivi->GiaBan * $soluong;
			$tenTivi_url = utility::utf8tourl($tivi->TenTivi); // ten tivi bỏ dấu , thay ký tự trắng bằng -
	?>
          
          <tr>
            <td><a href="<?php echo __URL_BASE.'tivi/chi-tiet-san-pham/'.$tivi->id.'/'.$tenTivi_url.'.html' ?>" title="<?php echo $tivi->TenTivi ?>" target="_blank"> 
            	<img src="<?php echo __URL_BASE.'images/'.$tivi->Images ?>" title="<?php echo $tivi->TenTivi ?>" alt="<?php echo $tivi->TenTivi ?>" width="170"> 
            </a></td>
            <td><a href="<?php echo __URL_BASE.'tivi/chi-tiet-san-pham/'.$tivi->id.'/'.$tenTivi_url.'.html' ?>" title="<?php echo $tivi->TenTivi ?>" target="_blank" style="color: #3D49D1;"> <?php echo $tivi->TenTivi ?> </a></td>
            <td style="text-align:center"><input type="number" value="<?php echo $soluong ?>" name="qty[<?php echo $tivi->id ?>]" min="0" max="999" /></td>
            <td><p class="price1"> <?php echo number_format($tivi->GiaBan,0,'','.') ?> đ </p>
              <p class="price2"> <?php echo number_format($tivi->GiaThat,0,'','.') ?> đ </p></td>
            <td><p class="price3"> <?php echo number_format($tivi->GiaBan * $soluong,0,'','.') ?> đ </p></td>
            <td><a class="delete" data-ajax="true" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-update="#m-update-cart" href="<?php echo __URL_BASE.'index.php?rt=cart/deletecart&tivi_id='.$tivi_id ?>">(Xóa)</a></td>
          </tr>
    <?php endforeach; ?>
          <tr>
          	<td colspan="2"></td>
          	<td  style="text-align:center;"><button type="submit" class="btn btn-success" >Cập nhật</button></td>
            <td colspan="3"><div style=" text-transform: uppercase; font: bold 13px Arial;float:left;padding:5px 0;"><span>Tổng giá trị thanh toán:</span></div>
            <p class="price3"> <?php echo number_format($TongTien,0,'','.') ?> đ </p></td>
          </tr>
        </tbody>
      </table>
	 </form>
    </div>
    <div class="cl"> </div>
   <form action="<?php echo __URL_BASE.'index.php?rt=cart/insert_order' ?>" method="post">
    <div class="p-cart-info">
    <h4 class="title"> <span>Thông tin khách hàng</span><span class="border"></span></h4>
    <table class="p-cart-customer">
      <tbody>
        <tr>
          <td valign="top" style="text-align:left;width:100px;"> Email<span class="require">(*)</span></td>
          <td><input name="UserName" type="text" value="" data-val-required="- Chưa nhập email?" data-val-regex-pattern="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$" data-val-regex="- Chưa đúng định dạng email?" data-val="true">
            <span class="field-validation-valid" data-valmsg-for="UserName" data-valmsg-replace="true"></span></td>
          <td valign="top"> Họ và tên<span class="require">(*)</span></td>
          <td><input type="text" name="FullName" data-val-required="- Chưa nhập họ và tên?" data-val="true">
            <span class="field-validation-valid" data-valmsg-for="FullName" data-valmsg-replace="true"></span></td>
          <td valign="top"> Số điện thoại<span class="require">(*)</span></td>
          <td><input type="text" name="Phone" data-val-required="- Chưa nhập số điện thoại?" data-val-regex-pattern="[0-9]+" data-val-regex="- Điện thoại phải là số!" data-val="true" class="num">
            <span class="field-validation-valid" data-valmsg-for="Phone" data-valmsg-replace="true"></span></td>
        </tr>
        <tr>
          <td valign="top"> Địa chỉ nhận hàng </td>
          <td colspan="5"><textarea name="Message" placeholder="Vui lòng cho chúng tôi biết thời gian nhận hàng trong ngày hoặc ghi chú thêm ....." data-val-required="- Chưa nhập địa chỉ nhận hàng?" data-val="true"></textarea>
            <span class="field-validation-valid" data-valmsg-for="Message" data-valmsg-replace="true"></span></td>
        </tr>
      </tbody>
    </table>
  </div>
    <div class="p-cart-info">
      <h4 class="title"> <span>Phương thức thanh toán</span><span class="border"></span></h4>
      <ul class="method-pay ls">
  <?php 
  		$i = 0;
		foreach($lstpttt as $item): ?>
        <li>
          <input type="radio" id="radioMethodPay<?php echo $item->id ?>" name="PaymentMethod" value="method<?php echo $item->id ?>" <?php if($i==0) echo 'checked="checked"' ?> onclick="javascript:showMethodPay('.radioMethodPay<?php echo $item->id ?>')">
          <?php echo $item->MoTa ?>
          <table class="radioMethodPay<?php echo $item->id ?>" style="display: <?php echo ($i==0) ? 'block' : 'none' ?>; border: none; width: 100%;">
            <tbody>
              <tr>
                <td><span>TỔNG GIÁ TRỊ ĐƠN HÀNG</span></td>
                <td><span>PHÍ DỊCH VỤ</span></td>
                <td><span>TỔNG TIỀN THANH TOÁN</span></td>
              </tr>
              <tr>
                <td> <?php echo number_format($TongTien,0,'','.') ?> đ</td>
                <?php $phidichvu = $item->PhiDichVu * $TongTien / 100 ?>
                <td><span>+<?php echo number_format($phidichvu,0,'','.') ?> đ</span></td>
                <td> <?php echo number_format($phidichvu + $TongTien,0,'','.') ?> đ </td>
              </tr>
             
            </tbody>
          </table>
        </li>
    <?php 
		$i++;
		endforeach; 
	?>   
      </ul>
      <div class="cl"> </div>
      <div class="p-cart-process">
      <?php if(isset($_SESSION['user'])) :?>
        		<input type="submit" id="btnOrder" value="Thanh toán đơn hàng">
      <?php else: ?>
        		<a class="fancybox" data-fancybox-type="iframe" href="<?php echo __URL_BASE.'index.php?rt=user/login&request=login' ?>" title="Đăng nhập" style="float: right;width: 140px;text-decoration: none;background-color: #ff0000;"> Thanh toán đơn hàng</a>
      <?php endif; ?>
      </div>
      <div class="cl"> </div>
    </div>
    </form>
<?php else: ?>
		<h3 style="font-size:20px;text-align:center;margin:50px">Hiện không có sản phẩm nào trong giỏ hàng.</h3>
<?php endif ?>
    <script type="text/javascript">
    function showMethodPay(obj) {
		$('table[class^=radioMethodPay]').css('display','none');
        $(obj).css({ "display": "block" });
    }
</script>
    <div class="cl"> </div>
  </div>
  <div class="cl"> </div>

<?php include FOOTER; ?>
<script type="text/javascript">
	$(document).ready(function() {
		$("input[type='number']").keydown(function(event) {
		// Allow: backspace, delete, tab, escape, and enter
			if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || 
			
			event.keyCode == 27 || event.keyCode == 13 || 
			// Allow: Ctrl+A
			(event.keyCode == 65 && event.ctrlKey === true) || 
			// Allow: home, end, left, right
			(event.keyCode >= 35 && event.keyCode <= 39)) {
			// let it happen, don't do anything
				return;
			}
			else {
			// Ensure that it is a number and stop the keypress
				if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && 
			
				(event.keyCode < 96 || event.keyCode > 105 )) {
					event.preventDefault(); 
				}   
			}
		});
	});
</script>